package cover

import (
	"path/filepath"

	"golang.org/x/tools/cover"
)

func TotalAvePercent(absProfilePath string) (total int, covered int, percent float64) {
	profiles, err := cover.ParseProfiles(filepath.Join(absProfilePath, "coverage.out"))
	if err != nil {
		return 0, 0, 0.0
	}

	for _, profile := range profiles {
		for _, block := range profile.Blocks {
			total += block.NumStmt
			if block.Count > 0 {
				covered += block.NumStmt
			}
		}
	}
	if total == 0 {
		return 0, 0, 0.0
	}

	return total, covered, float64(covered) / float64(total) * 100
}
